package com.mimosa.common.view;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.mimosa.common.entity.BaseEntity;
import com.mimosa.util.constant.StringConstUtil;

/**
 * 将Page对象包含的对象列表转化为JQueryEasyUI的列表对象能接受的格式 <BR>
 * 格式示例如下所示： [{ "text":"机场部门列表", "state":"open", "children":[{ "id":"1",
 * "text":"技术部", "state":"closed", "children":[{ "id":"11", "text":"技术1部" },{
 * "id":"12", "text":"技术2部" }] },{ "id":"2", "text":"行政部", "state":"closed",
 * "children":[{ "id":"21", "text":"行政1部" },{ "id":"22", "text":"行政2部" }] }] }]
 * 
 * @date 2012-3-13
 * @filename JqueryEasyUiGrid.java
 * @author Yanlei Han
 */
public abstract class NoLazyEasyUiTreeData<T extends BaseEntity> {
	/**
	 * 根结点信息
	 */
	private T root;
	protected StringBuilder builder = null;

	public NoLazyEasyUiTreeData(T root) {
		this.root = root;
		this.builder = new StringBuilder();
	}

	protected abstract boolean userAttribute();

	protected abstract String[] getAttributeNames();

	protected abstract String[] getAttributeValues(T entity);

	protected abstract String[] getColumnNames();

	protected abstract String[] getColumnValues(T entity);

	void formatEntity(T entity) {
		this.builder.append(StringConstUtil.LEFT_BRACE);
		String[] values = this.getColumnValues(entity);
		int index = 0;
		String value = StringUtils.EMPTY;
		for (String name : this.getColumnNames()) {
			if (values[index] != null) {
				value = values[index];
				value = StringUtils.replace(value, StringConstUtil.DOUBLE_QUOTE, "\\\"");
				value = StringUtils.replace(value, StringConstUtil.SINGLE_QUOTE, "\\\'");
				value = StringUtils.remove(value, '\r');
				value = StringUtils.remove(value, '\n');
			} else {
				value = StringUtils.EMPTY;
			}
			this.appendNameValue(name, value);
			index++;
		}
		// 设置属性信息
		index = 0;
		if (this.userAttribute()) {
			this.appendName("attributes");
			this.builder.append(StringConstUtil.LEFT_BRACE);
			values = this.getAttributeValues(entity);
			for (String name : this.getAttributeNames()) {
				if (values[index] != null) {
					value = values[index];
				} else {
					value = StringUtils.EMPTY;
				}
				this.appendNameValue(name, value);
				index++;
			}
			this.builder.deleteCharAt(this.builder.length() - 1);
			this.builder.append(StringConstUtil.RIGHT_BRACE);
		} else {
			this.builder.deleteCharAt(this.builder.length() - 1);
		}
		this.builder.append(StringConstUtil.RIGHT_BRACE);
	}

	/**
	 * 将参数名/值对追加到数组中
	 * 
	 * @param name
	 * @param value
	 */
	void appendNameValue(String name, String value) {
		this.builder.append(StringConstUtil.DOUBLE_QUOTE).append(name).append(StringConstUtil.DOUBLE_QUOTE);
		this.builder.append(StringConstUtil.COLON);
		if ("true".equals(value) || "false".equals(value)) {
			this.builder.append(value);
		} else {
			this.builder.append(StringConstUtil.DOUBLE_QUOTE).append(value).append(StringConstUtil.DOUBLE_QUOTE);
		}
		this.builder.append(StringConstUtil.COMMA);
	}

	/**
	 * 将参数名追加到当前的字符串数组中
	 * 
	 * @param name
	 */
	void appendName(String name) {
		this.builder.append(StringConstUtil.DOUBLE_QUOTE).append(name).append(StringConstUtil.DOUBLE_QUOTE);
		this.builder.append(StringConstUtil.COLON);
	}

	protected void toGridFormat(T node) {
		// 父节点信息
		this.formatEntity(node);
		this.builder.deleteCharAt(this.builder.length() - 1);
		// 子节点信息
		List<T> children = this.getChildNodes(node);
		if (children != null && children.size() > 0) {
			this.builder.append(StringConstUtil.COMMA).append(StringConstUtil.DOUBLE_QUOTE).append("children").append(StringConstUtil.DOUBLE_QUOTE).append(StringConstUtil.COLON);
			this.builder.append(StringConstUtil.LEFT_SQUARE_BRACKET);
			for (T child : children) {
				this.toGridFormat(child);
				this.builder.append(StringConstUtil.COMMA);
			}
			this.builder.deleteCharAt(this.builder.length() - 1);
			this.builder.append(StringConstUtil.RIGHT_SQUARE_BRACKET);
		}
		this.builder.append(StringConstUtil.RIGHT_BRACE);
	}

	protected abstract List<T> getChildNodes(T node);

	@Override
	public String toString() {
		if (this.builder.length() == 0) {
			this.builder.append(StringConstUtil.LEFT_SQUARE_BRACKET);
			this.toGridFormat(this.root);
			this.builder.append(StringConstUtil.RIGHT_SQUARE_BRACKET);
		}
		return builder.toString();
	}
}